package com.zyc.web;

import com.zyc.service.RequestService;
import com.zyc.vo.RequestInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@Slf4j
public class NavController {

    // 注解请求信息Service
    @Autowired
    private RequestService requestService;

    @GetMapping("/")
    public String list(ModelMap model) {
        log.info("获取请求信息数据");
        // 到数据库查找请求记录信息
        List<RequestInfo> requestInfoIPList = requestService.getRequestInfoIPList();
        if(null == requestInfoIPList || requestInfoIPList.size() < 1) {
            requestInfoIPList = new ArrayList<>();
            // 返回空的请求记录信息,在第一次访问时
            model.addAttribute("requestInfoIPList", requestInfoIPList);
        }else {
            // 返回请求记录信息
            model.addAttribute("requestInfoIPList", requestInfoIPList);
        }
        List<RequestInfo> requestInfoTypeList = requestService.getRequestInfoTypeList();
        if(null == requestInfoTypeList || requestInfoTypeList.size() < 1) {
            requestInfoTypeList = new ArrayList<>();
            // 返回空的请求记录信息,在第一次访问时
            model.addAttribute("requestInfoTypeList", requestInfoTypeList);
        }else {
            // 返回请求记录信息
            model.addAttribute("requestInfoTypeList", requestInfoTypeList);
        }

        // 此处返回的index字符串,对应前端templates目录下的index.html名字
        return "list";
    }

    @GetMapping("/insert")
    public String index(ModelMap model) {

        return "index";
    }

}
